package argo.hot100.substr;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.LinkedList;

public class MaxSlidingWindow {

    public int[] maxSlidingWindow(int[] nums, int k) {
        int[] res = new int[nums.length - k + 1];
        Deque<Integer> deque = new LinkedList<>();
        int left = 0, right = k;
        while (left < k) {
            deque.offerLast(nums[left]);
            left++;
        }
        res[0] = getMax(deque);
        left = 1;
        while (right < nums.length) {
            deque.poll();
            deque.offer(nums[right]);
            res[left] = getMax(deque);
            left++;
            right++;
        }

        return res;

    }


    public int getMax(Deque<Integer> deque) {
        Integer max = deque.stream().max(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        }).get();
        return max;
    }


    public static void main(String[] args) {
        System.out.println(Arrays.toString(new MaxSlidingWindow().maxSlidingWindow(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3)));
    }
}
